package com.wanxing.sellerapp.emqx.handlers

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.wanxing.sellerapp.App
import com.wanxing.sellerapp.emqx.entity.EmqMessage
import com.wanxing.sellerapp.emqx.entity.EmqPayload
import com.wanxing.sellerapp.emqx.enums.ActionEnum
import com.wanxing.sellerapp.emqx.interfaces.IMessenger
import com.wanxing.sellerapp.seller.SellerManager
import com.wanxing.sellerapp.seller.callback.OpenDoorCallback
import com.wanxing.sellerapp.seller.entity.ScaleWeight
import com.wanxing.sellerapp.seller.events.ShowLogEvent
import org.greenrobot.eventbus.EventBus

class OpenDoorHandler(messenger: IMessenger, sellerManager: SellerManager) : AbstractActionHandler(messenger, sellerManager) {

    override fun handleAction(messageId: String?, data: JSONObject, payload: EmqPayload) {
        val doorNo = data.getIntValue("doorNo")
        val orderId = data.getString("orderId")
        EventBus.getDefault().post(ShowLogEvent("补货开门", orderId))
        sellerManager.seller.openDoor(doorNo, orderId, object :OpenDoorCallback{
            override fun onGetStableWeights(weightList: MutableList<ScaleWeight>?) {
                //todo 发送开门时重量
                val message = EmqMessage()
                message.action = ActionEnum.OPEN_DOOR
                message.doorNo= doorNo
                message.timestamp = System.currentTimeMillis()
                message.messageId = messageId
                message.mid = App.mid
                val sendData = JSONObject()
                sendData["weights"] = weightList
                message.data = sendData
                messenger.sendMsgToService(JSON.toJSONString(message), true, 2)
            }

            override fun videoOpenState(state: String) {
                val message = EmqMessage()
                message.action = ActionEnum.OPEN_DOOR
                message.doorNo= doorNo
                message.timestamp = System.currentTimeMillis()
                message.messageId = messageId
                message.mid = App.mid
                val sendData = JSONObject()
                sendData["type"] = state
                message.data = sendData
                messenger.sendMsgToService(JSON.toJSONString(message), false, 2)
            }
        })
    }



}